﻿using Anke.SHManage.Model;
using Anke.SHManage.MSSQLDAL;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;

namespace Anke.SHManage.BLL
{
    public partial class P_UserBLL : BaseBLL<P_User>
    {
        private P_UserDAL dal = new P_UserDAL();

         //获取所有人员 （combobox用）   
        public List<P_User> GetUserList()
        {
            return dal.GetUserList();
        }
         /// <summary>
        /// 将 权限集合 转成 树节点集合
        /// </summary>
        /// <param name="listPer"></param>
        /// <returns></returns>
        public  List<TreeNode> ToTreeNodes(string rootID,string rootName)
        {
            string sql = @" select * from P_Department where IsActive=1";
            List<P_Department> list=base.DALContext.IP_DepartmentDAL.ExcuteSqlToList(sql);
            if (list.Count > 0)
            {
                if (list[0].ID == list[0].ParentID)
                {
                    list[0].ParentID = -1;
                }
            }
            List<TreeNode> listNodes = new List<TreeNode>();
            listNodes.Add(new TreeNode(rootName, rootID));

            //生成 树节点时，根据 pid=0的根节点 来生成
            LoadTreeNode(list, listNodes, rootID);
            return listNodes;
        }

        /// <summary>
        /// 递归权限集合 创建 树节点集合
        /// 无级递归
        /// </summary>
        /// <param name="listPer">权限集合</param>
        /// <param name="listChildren">节点集合</param>
        /// <param name="pID">节点父id</param>
        protected  void LoadTreeNode(List<P_Department> list, List<TreeNode> listChildrenNode, string pID)
        {
            foreach (var dep in list)
            {
                //判断权限ParentID 是否和 传入参数相等
                if (dep.ParentID.ToString().Equals(pID))
                {
                    //将 权限转成 树节点
                    TreeNode node = new TreeNode(dep.Name,dep.ID.ToString());
                    //将节点加入到 树子节点集合
                    listChildrenNode.Add(node);

                    //递归 为这个新创建的 树节点找 子节点
                    LoadTreeNode(list, listChildrenNode, dep.ID.ToString());

                }
            }
        }
        // 查询所有人员    
        public object GetUserList(int page, int rows, ref int rowCounts, string strIsActive, string DiseaseID)
        {
            return dal.GetUserList(page, rows, ref rowCounts, strIsActive, DiseaseID);
        }
        public object GetUserList(int page, int rows, ref int rowCounts, string workID, string name
             , string searchBound, string selworkID, string depIDs)
        {
            return dal.GetUserList(page, rows, ref rowCounts, workID, name, searchBound, selworkID, depIDs);
        }
        //取P_User表中id最大的一条数据
        public List<P_User> GetMaxID()
        {
            string sql = @" select * from P_User where ID in (select max(ID) from P_User)";
            return base.DALContext.IP_UserDAL.ExcuteSqlToList(sql);
        }

        /// <summary>
        /// 获取带教人员下拉字典 （combobox用）   
        /// </summary>
        /// <returns></returns>
        public object GetTeachUserDic()
        {
            return dal.GetTeachUserDic();
        }

        /// <summary>
        /// 获取对应人员的带教医生
        /// </summary>
        /// <param name="workcode"></param>
        /// <returns></returns>
        public object GetUserGroup(string workcode)
        {
            return dal.GetUserGroup(workcode);
        }
        /// <summary>
        /// 保存对应人员的带教医生配置
        /// </summary>
        /// <param name="workcode"></param>
        /// <param name="pworkcode"></param>
        /// <returns></returns>
        public bool SaveUserGroup(string workcode, string pworkcode)
        {
            return dal.SaveUserGroup(workcode, pworkcode);
        }
    }
}
